package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.suffix;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.ExtMyBatis3FormattingUtilities;
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.SuffixUtil;
import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;

import java.util.List;

/*********************************************************
 * 文件名称：SuffixInsertBatchElementGenerator.java
 * 系统名称：cxycloud
 * 模块名称：org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.suffix
 * 功能说明：带后缀的批量插入生成器
 * 开发人员 @author：caoxy
 * 开发时间 @date：2023/8/29 10:57
 * 修改记录：程序版本  修改日期  修改人员  修改单号  修改说明
 *********************************************************/
public class SuffixInsertBatchElementGenerator extends AbstractXmlElementGenerator {


    public void addElements(XmlElement parentElement) {
        XmlElement answer = new XmlElement("insert");
        answer.addAttribute(new Attribute("id", SuffixUtil.getSuffixMethodName(ExtMyBatis3FormattingUtilities.ATTR_INSERT_BATCH_STATEMENT_ID, introspectedTable)));
        answer.addAttribute(new Attribute("parameterType", "map"));
        this.context.getCommentGenerator().addComment(answer);
        List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(this.introspectedTable.getAllColumns());
        XmlElement foreachElement = new XmlElement("foreach");
        foreachElement.addAttribute(new Attribute("collection", "rows"));
        foreachElement.addAttribute(new Attribute("item", "row"));
        foreachElement.addAttribute(new Attribute("separator", ","));
        foreachElement.addElement(new TextElement("(<include refid=\"Insert_Column_List\" />)"));
        XmlElement foreachElement2 = new XmlElement("foreach");
        foreachElement2.addAttribute(new Attribute("collection", "rows"));
        foreachElement2.addAttribute(new Attribute("item", "row"));
        foreachElement2.addElement(new TextElement("into " + SuffixUtil.addTableNameSuffix(this.introspectedTable.getFullyQualifiedTableNameAtRuntime(),introspectedTable)));
        foreachElement2.addElement(new TextElement("("));
        foreachElement2.addElement(getBaseColumnListElement());
        if (introspectedTable.hasBLOBColumns()){
            foreachElement2.addElement(new TextElement(",")); //$NON-NLS-1$
            foreachElement2.addElement(getBlobColumnListElement());
        }
        foreachElement2.addElement(new TextElement(")"));
        foreachElement2.addElement(new TextElement("values"));
        foreachElement2.addElement(new TextElement("(<include refid=\"Insert_Column_List\" />)"));
        XmlElement mysqlElement = new XmlElement("when");
        mysqlElement.addAttribute(new Attribute("test", "${getDBType} == 'Mysql' or ${getDBType} == 'OceanBase'"));
        mysqlElement.addElement(new TextElement("insert into " + SuffixUtil.addTableNameSuffix(this.introspectedTable.getFullyQualifiedTableNameAtRuntime(),introspectedTable)));
        mysqlElement.addElement(new TextElement("("));
        mysqlElement.addElement(getBaseColumnListElement());
        if (introspectedTable.hasBLOBColumns()){
            mysqlElement.addElement(new TextElement(",")); //$NON-NLS-1$
            mysqlElement.addElement(getBlobColumnListElement());
        }
        mysqlElement.addElement(new TextElement(")"));
        mysqlElement.addElement(new TextElement("values"));
        mysqlElement.addElement(foreachElement);
        XmlElement otherElement = new XmlElement("otherwise");
        otherElement.addElement(new TextElement("insert all"));
        otherElement.addElement(foreachElement2);
        otherElement.addElement(new TextElement("select 1 from dual"));
        XmlElement chooseElement = new XmlElement("choose");
        chooseElement.addElement(mysqlElement);
        chooseElement.addElement(otherElement);
        answer.addElement(chooseElement);
        if (this.context.getPlugins().sqlMapInsertElementGenerated(answer, this.introspectedTable)
                && SuffixUtil.isEnableSuffix(introspectedTable)) {
            SuffixUtil.deleteOldElement(parentElement,answer);
            parentElement.addElement(answer);
        }

    }
}
